#pragma once
#include "stdafx.h"

TList* StringTokenize(const char* mystring, const char* delimiters, const char* symbols)
{
	TList*list = new TList();
	
	int lenA=strlen(mystring);if (lenA<1) return list;
	int lenB=strlen(delimiters);
	int lenC=strlen(symbols);
	bool UseDelim=(lenB>0);
	bool UseSymbol=(lenC>0);
	if (!UseDelim && !UseSymbol) return list;

	int a=0,b=0,c=0, a1=0;
	char ca=0,cb=0;
	
	for (a=0;a<lenA;a++)
	{
		ca=mystring[a];

		bool found=false;
		
		if (UseDelim)
		{
			for (b=0;b<lenB;b++)
			{
				if (delimiters[b]==ca)
				{
					if ((a-a1)>0)
					{
						char* dest = (char*)malloc((a-a1+1)*sizeof(char));
						int i=0;
						for (i=a1;i<a;i++){dest[i-a1]=mystring[i];};
						dest[a-a1]='\0';
						AddLast(list,dest);
					};
					a1=a+1;found=true;break;
				};
			};
		};
		
		if (UseSymbol && found==false)
		{
			for (c=0;c<lenC;c++)
			{
				if (symbols[c]==ca)
				{
					if ((a-a1)>0)
					{
						char* dest = (char*)malloc((a-a1+1)*sizeof(char));
						int i=0;
						for (i=a1;i<a;i++){dest[i-a1]=mystring[i];};
						dest[a-a1]='\0';
						AddLast(list,dest);
					};
					
					char * dest = (char*)malloc((2)*sizeof(char));
					dest [0] = symbols[c];
					dest [1] = '\0';
					AddLast(list,dest);
					a1=a+1;found=true;break;
				};
			};
		};
		
		if (a==lenA-1 && found==false)
		{
			char* dest = (char*)malloc((a-a1+2)*sizeof(char));
			int i=0;
			for (i=a1;i<a+1;i++){dest[i-a1]=mystring[i];};
			dest[a-a1+1]='\0';
			AddLast(list,dest);
		};
	};
	return list;
}

/*
const char* CharToMorse(char c)
{

	int off;
	char *dida;
	dida="";

	if (isalpha(c))
	{
	   if (islower(c))
		  {
			  off = c-'a';
			  dida = code[off];
		   }
	   if (isupper(c))
	  {
			  off = c-'A';
			  dida=code[off];
		  }
	}

	if (isdigit(c))
	{
	  off = c-'0';
	  dida = num[off];
	}
	off=-1;
	if (ispunct(c))
	{
	  if (c == '/') off=0;
	  if (c == ',') off = 1;
	  if (c == '.') off = 2;
	  if (c == '?') off = 3;
	  if (c == '=') off = 4;
	  if (c == '+') off = 5;
	  if (c == '#') off = 6;
	  if (c == '$') off = 7;
	  // if we don't recognize the character, make it a ?
	  if (off < 0) off=3;
	  dida = punct[off];
	}

	// take care of stray unknowns
	if (dida[0]=='\0')
	dida=punct[3];

	while (*dida != '\0')
	{
	  if (*dida == '.')
	out_tone(secs_per_tick,freq,SRATE,outfil);
	  else
	out_tone(3*secs_per_tick,freq,SRATE,outfil);
	  out_space(intrachar_space,freq,SRATE,outfil);
	  dida++;
	}
	out_space(interchar_space-intrachar_space,freq,SRATE,outfil);

};
const char* StrToMorse(const char* in)
{



	int ln=strlen(tmp);
	const char *out = (const char*) malloc(ln*6*sizeof(char));
	int c=0;
	for(c==0; c<ln;c++)
	{
		out[c]=in[c];
	};
	return out;
};
const char* StrFromMorse(const char* in)
{
	return toupper(in);
};
*/

char  *code[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
	       "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
	       "..-","...-",".--","-..-","-.--","--.."};

char *num[]={"-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};

char *punct[]={	"-..-.", /* slash */ 
				"--..--", /* comma */
				".-.-.-", /* period */
				"..--..", /* question */
				"-...-",/*BT  = */
				".-.-.",/*AR  +*/
				"-.--.",/* KN #*/ 
				"...-.-"};/*  SK $*/
